package com.cysd.spsyxj.wxapi

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.csgc.camera.constants.Constants
import com.csgc.camera.events.WXAuthCodeBindEvents
import com.csgc.camera.events.WXAuthCodeEvents
import com.csgc.camera.util.WXWrapper
import com.drake.channel.sendEvent
import com.tencent.mm.opensdk.constants.ConstantsAPI
import com.tencent.mm.opensdk.modelbase.BaseReq
import com.tencent.mm.opensdk.modelbase.BaseResp
import com.tencent.mm.opensdk.modelmsg.SendAuth
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler

class WXEntryActivity:AppCompatActivity(),IWXAPIEventHandler {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        WXWrapper.instance.wxApi.handleIntent(intent,this)
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        WXWrapper.instance.wxApi.handleIntent(intent,this)
    }

    override fun onReq(req: BaseReq?) {

    }

    override fun onResp(resp: BaseResp?) {
        if (resp!=null){
            if (resp.type == ConstantsAPI.COMMAND_SENDAUTH){
                //登录 or 绑定
                val authResp = resp as SendAuth.Resp
                if (authResp.state == Constants.WXAuthType.LOGIN.state){
                    //登录
                    sendEvent(WXAuthCodeEvents(authResp.code,authResp.openId))
                }else if (authResp.state == Constants.WXAuthType.BIND.state){
                    //绑定
                    sendEvent(WXAuthCodeBindEvents(authResp.code))
                }


            }
        }



        finish()
    }
}